package com.maze;
import java.io.*;
import java.util.*;

public class ReadCsv {
	 /**
     * <code>readToList</code> method to read the Csv file stored in list.
     * 
     * @param file File's path
     * @return the list of data
     */
    public static List<String> readToList(File file) {
    	List<String> dataList=new ArrayList<String>();
        BufferedReader br=null; 
        try {
        	br = new BufferedReader(new FileReader(file));
            String dataLine = "";
            while ((dataLine = br.readLine()) != null) {
                dataList.add(dataLine);
            }
        } catch (Exception e) {
        } finally {
        	if(br!=null){
        		try {
                     br.close();
                     br=null;
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
        	}
        }
        return dataList;
    }
    
    /**
     * <code>readCsv</code> method to store list into map.
     * 
     * @param file File's path
     * @return the Map of data
     */
    public static Map <Position,CELL> readCsv(File file){
        List<String> dataList= readToList(file);
        Map <Position,CELL> cell = new HashMap<Position,CELL>();
        int row = dataList.size();
        String str = dataList.get(0);
        int column =  str.split(",",str.length() + 1).length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                String rowString = dataList.get(i);
                String item = rowString.split(",", rowString.length() + 1)[j];
                Position p = new Position(i,j);
                switch (item) {
                    case "W" :
                        cell.put(p, CELL.CELL_W);
                        break;
                    case "" :
                        cell.put(p, CELL.CELL_E);
                        break;
                    case "T" :
                    	cell.put(p, CELL.CELL_T);
                    	break;
                    case "L" :
                    	cell.put(p, CELL.CELL_L);
                    	break;
                    default: 
                        System.out.println("wrong input!");
                        System.out.close();
                }
            }
        }
        return cell;
    }

    public static List<String> readCsv2List(File file){
        List<String> dataList=new ArrayList<String>();

        BufferedReader br=null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dataList;
    }

    public static int[][] readCsv2Array(File file){
        List<String> dataList= readCsv2List(file);
        int row = dataList.size();
        String str = dataList.get(0);
        int column =  str.split(",",str.length() + 1).length;
        int[][] retArray = new int[row][column];
        for (int i = 0 ;i< row;i++) {
            for (int j=0;j<column;j++) {
                String rowString = dataList.get(i);
                String item = rowString.split(",", rowString.length() + 1)[j];
                switch (item) {
                    case "" :
                        retArray[i][j] = 1;
                        break;
                    case "W" :
                        retArray[i][j] = 0;
                        break;
                    case "w" :
                        retArray[i][j] = 0;
                        break;
                }
            }
        }
        return retArray;
    }
}